package com.bootdo.system.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.config.Constant;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.vo.UserVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.TUserDO;
import com.bootdo.system.service.TUserService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 用户表
 * 
 * @author Ace Lee
 * @email las1990@126.com
 * @date 2019-11-16 20:34:14
 */
 
@Controller
@RequestMapping("/system/tUser")
public class TUserController {
	private String prefix="system/tUser"  ;
	@Autowired
	private TUserService tUserService;
	
	@GetMapping()
	@RequiresPermissions("system:tUser:tUser")
	String TUser(){
	    return "system/tUser/tUser";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:tUser:tUser")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<TUserDO> tUserList = tUserService.list(query);
		int total = tUserService.count(query);
		PageUtils pageUtils = new PageUtils(tUserList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:tUser:add")
	String add(){
	    return "system/tUser/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:tUser:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		TUserDO tUser = tUserService.get(id);
		model.addAttribute("tUser", tUser);
	    return "system/tUser/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:tUser:add")
	public R save( TUserDO tUser){
		if(tUserService.save(tUser)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:tUser:edit")
	public R update( TUserDO tUser){
		tUserService.update(tUser);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:tUser:remove")
	public R remove( Integer id){
		if(tUserService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:tUser:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		tUserService.batchRemove(ids);
		return R.ok();
	}

	@RequiresPermissions("system:tUser:verify")
	@Log("设置审核人员")
	@GetMapping("/verify/{id}")
	String verify(@PathVariable("id") Integer id, Model model) {

		TUserDO userDO = tUserService.get(id);
		model.addAttribute("tUser", userDO);
		return prefix + "/verify";
	}

	@RequiresPermissions("system:tUser:verify")
	@Log("设置审核人员")
	@PostMapping("/setVerify")
	@ResponseBody
	R verify(TUserDO userVO) {
		try{
			tUserService.verify(userVO.getId());
			return R.ok();
		}catch (Exception e){
			return R.error(1,e.getMessage());
		}

	}
	
}
